package com.facebookproject.graph;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebookproject.R;
import com.facebookproject.graph.element.ElementAdapterGridView;
import com.facebookproject.graph.element.ElementAdapterListView;
import com.facebookproject.graph.getlist.GetListTask;
import com.facebookproject.graph.showdialogdetail.ShowDialog;
public class GraphSearchActivity extends ListActivity  {

	ElementAdapterListView adapter_list;
	ElementAdapterGridView adapter_grid;
	Button back,listviewbtm,gridviewbtm;
	ProgressBar prgBar;
	ListView listView;
	GridView gridView;
	Holder fbHolder = new Holder();
	int type,tab;
	Context context = this;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.graph_search_activity);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		back = (Button)findViewById(R.id.graph_back);
		listView = getListView();
		gridView = (GridView)findViewById(R.id.grid);
		listviewbtm = (Button)findViewById(R.id.graph_listview);
		gridviewbtm = (Button)findViewById(R.id.graph_gridview);
		prgBar = (ProgressBar)findViewById(R.id.pgBarGraph);
		listviewbtm.setClickable(false);
		gridviewbtm.setClickable(true);

		Intent it = getIntent();
		type = it.getIntExtra("type", 1);
		tab = it.getIntExtra("tab",1);


		DisplayMetrics dpi = context.getResources().getDisplayMetrics();
		TextView test = (TextView)findViewById(R.id.test);
		String teststr = Float.toString(dpi.density) + "\t" +Integer.toString(dpi.densityDpi)
				+ "\t"+ Integer.toString(dpi.widthPixels);
		test.setText(teststr);

		
		//Get list element and set it on adapter_list
		getfbHolder();
		new GetListTask(fbHolder).execute();

		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				new GetListTask(fbHolder).cancel(true);
				finish();
			}
		});
		listviewbtm.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				listviewbtm.setClickable(false);
				gridviewbtm.setClickable(true);
				setadapter_list();
			}
		});
		gridviewbtm.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				listviewbtm.setClickable(true);
				gridviewbtm.setClickable(false);
				setadapter_grid();

			}
		});

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long arg3) {
				String id = fbHolder.adapter_list.getItem(pos).getId();
				new ShowDialog(id, context);
			}
		});

		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long arg3) {
				String id = fbHolder.adapter_list.getItem(pos).getId();
				new ShowDialog(id, context);
			}
		});
	}
	private void setadapter_list() {
		gridView.setAdapter(null);
		setListAdapter(fbHolder.adapter_list);
	}
	private void setadapter_grid() {
		setListAdapter(null);
		gridView.setAdapter(fbHolder.adapter_grid);
	}
	private void getfbHolder(){
		fbHolder.listView = listView;
		fbHolder.gridView = gridView;
		fbHolder.adapter_list = adapter_list;
		fbHolder.adapter_grid = adapter_grid;
		fbHolder.prgBar = prgBar;
		fbHolder.context = this;
		fbHolder.type = type;
		fbHolder.tab = tab;
	}
	@Override
	public void onBackPressed() {
		new GetListTask(fbHolder).cancel(true);
		finish();
		super.onBackPressed();
	}
}
